﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace DAL
{
   public class DBHelper
    {
        public static string connString = ConfigurationManager.ConnectionStrings["LogisticsDB"].ConnectionString;
        public static SqlDataReader ExecuteReader(string sql,CommandType type=CommandType.Text,params SqlParameter[] pams)
        {
            //连接数据库
            SqlConnection con = new SqlConnection(connString);
           
            //声明命令
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                //添加参数
                if(pams!=null && pams.Length > 0)
                {
                    cmd.Parameters.AddRange(pams);
                }
                //打开数据库连接
                if (con.State == ConnectionState.Closed)
                {
                    //打开数据库连接
                    con.Open();
                }
                //执行存储过程
                cmd.CommandType = type;
                //执行命令
                return cmd.ExecuteReader();
            }
           
        }


        public static int ExecuteNonQuery(string sql, CommandType type = CommandType.Text, params SqlParameter[] pams)
        {
            //连接数据库
            SqlConnection con = new SqlConnection(connString);

            //声明命令
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                //添加参数
                if (pams != null && pams.Length > 0)
                {
                    cmd.Parameters.AddRange(pams);
                }
                //打开数据库连接
                if (con.State == ConnectionState.Closed)
                {
                    //打开数据库连接
                    con.Open();
                }
                //执行存储过程
                cmd.CommandType = type;
                //执行命令
                return cmd.ExecuteNonQuery();
            }

        }




        public static object ExecuteScalar(string sql, CommandType type = CommandType.Text, params SqlParameter[] pams)
        {
            //连接数据库
            SqlConnection con = new SqlConnection(connString);

            //声明命令
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                //添加参数
                if (pams != null && pams.Length > 0)
                {
                    cmd.Parameters.AddRange(pams);
                }
                //打开数据库连接
                if (con.State == ConnectionState.Closed)
                {
                    //打开数据库连接
                    con.Open();
                }
                //执行存储过程
                cmd.CommandType = type;
                //执行命令
                return cmd.ExecuteScalar();
            }

        }


        public static DataTable da(string sql)
        {
            SqlDataAdapter adapter = new SqlDataAdapter();
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            return dt;
        }
    }
}
